package sim.tests.games.alone;

import java.util.Random;

import javax.tools.JavaCompiler;

import sim.Image;
import sim.Sound;
import sim.Sprite;
import sim.input.Mouse;
import sim.misc.Holder;
import sim.states.BasicGame;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxRuntimeException;

public class Game extends BasicGame
{
	Array<sim.Sprite> images;
	Jar jar;
	Sound collect;
	Sprite background;
	
	

	@Override
	public void init()
	{
		
		
		collect = new Sound("collect.wav");
		setWorldSize(200, 150);
		
		images = new Array<sim.Sprite>();
		
		jar = new Jar(new Vector2(0, 0));
		jar.image.setSize(64, 64);
		
		images.add(jar);
		
		
		background = new Sprite(new Image("backSmall.png"), 0, 0);
		background.image.setSize(200, 150);
		
		
	
		
		
		Random rad = new Random();

		for (int x = 0; x < 10; x++)
		{

			Soul soul = new Soul((float) rad.nextInt(200), (float) rad.nextInt(150));
			soul.image.setRotation(rad.nextInt(360));
			soul.image.setSize(32, 32);
			soul.vel.x = rad.nextInt(50) + 2;
			soul.vel.y = rad.nextInt(50) + 2;

			if (rad.nextInt(2) == 0)
				soul.vel.x *= -1;

			if (rad.nextInt(2) == 0)
				soul.vel.y *= -1;

			images.add(soul);
		}

	}

	@Override
	public void update(float delta)
	{
		
		for (sim.Sprite s : images)
		{
			if(s instanceof Soul)
				
			if (s.isTouched(camera))
				{
					//The destination Vector
					Vector2 destination = new Vector2(0, 0);
					
					 s.vel = destination.sub(s.pos);
					float hyp = (float) Math.sqrt(s.vel.x*s.vel.x + s.vel.y * s.vel.y);

					s.vel.x /= hyp;
					s.vel.y /= hyp;
					
					s.vel.mul(100);
					
					((Soul) s).clicked = true;
					
					
					s.vel = s.vel;
					// images.removeValue(s, false);
					
				}
			
			
			if(s.bounds.overlaps(jar.bounds))
			{
				if(s instanceof Soul)
					if(((Soul) s).clicked)
					{
						collect.play();
						s.pos.x = -100;
						s.pos.y = -100;
						s.vel.x = 0;
						s.vel.y = 0;
						//images.removeValue(s, false);
						jar.shake();
					}
			}

			s.update(delta);
		}

	}

	@Override
	public void draw()
	{
		Holder.b.disableBlending();
		background.draw();
		Holder.b.enableBlending();
		for (sim.Sprite s : images)
			s.draw();

	}

}
